﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using System.IO;
using MITI;
using System.Diagnostics; 

namespace WBrowser.MY_CODE
{
    public partial class ReadTemplateData : Form
    {
        private DataSet _dataset;

        #region ReadTemplateData
        public ReadTemplateData()
        {
            InitializeComponent();
            
        }
        #endregion

        #region cmdLoadDataGrid_Click
        private void LoadDataGrid1()
        {
            SqlCeConnection cn = new SqlCeConnection(GeneralClass.ConnectString());
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
            try
            {
                // Set the command to use the table, not a query
                SqlCeCommand cmd = new SqlCeCommand("Templates", cn);
                cmd.CommandType = CommandType.TableDirect;

                // Get the table
                SqlCeResultSet rs = cmd.ExecuteResultSet(
                ResultSetOptions.Scrollable);

                // load the result set into the datasource
                dataGridViewCoolPeople.DataSource = rs;
            }

            catch (SqlCeException sqlexception)
            {
                MessageBox.Show(sqlexception.Message, "Oh Crap.",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Oh Crap.",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // Note, do not close the connection,
            // if you do the grid won't be able to display.
            // For production code you probably want to make
            // your result set (rs) a class level variable
        }

        private void LoadDataGrid2()
        {
            try
            {
                _dataset = BaseDBCe.ExecSql_DataSet("select * from Templates");
                dataGridViewCoolPeople.DataSource = _dataset.Tables[0].DefaultView;
                
            }
            catch (SqlCeException sqlexception)
            {
                MessageBox.Show(sqlexception.Message, "Oh Crap.",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Oh Crap.",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // Note, do not close the connection,
            // if you do the grid won't be able to display.
            // For production code you probably want to make
            // your result set (rs) a class level variable
        }

        private void cmdLoadDataGrid_Click(object sender, EventArgs e)
        {
            LoadDataGrid2();
        }

        #endregion

        private void ReadTemplateData_Load(object sender, EventArgs e)
        {
            BaseDBCe.g_ConnectStr = GeneralClass.ConnectString();
            // TODO: This line of code loads data into the 'mpconfigDataSet.Templates' table. You can move, or remove it, as needed.
            this.templatesTableAdapter.Connection.ConnectionString = BaseDBCe.g_ConnectStr;
            this.templatesTableAdapter.Fill(this.mpconfigDataSet.Templates);
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            int indexRow = dataGridViewCoolPeople.SelectedCells[0].RowIndex;
            Process.Start("firefox.exe", dataGridViewCoolPeople.Rows[indexRow].Cells[1].Value.ToString());
        }

        private void btnCheckAlive_Click(object sender, EventArgs e)
        {

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.templatesBindingSource.EndEdit();
            this.templatesTableAdapter.Update(this.mpconfigDataSet.Templates);
            MessageBox.Show("Cập nhật xong.");
        }

        private void btnGetLink_Click(object sender, EventArgs e)
        {

        }

       
    }
}